package com.tansun.easycare.rule.modelauthority.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jeedev.msdp.base.entity.SysUser;
import com.tansun.easycare.modules.sys.utils.UserUtils;
import com.tansun.easycare.rule.common.RuleBaseController;
import com.tansun.easycare.rule.common.model.BaseModel;
import com.tansun.easycare.rule.common.model.ResponseVO;
import com.tansun.easycare.rule.exception.BizException;
import com.tansun.easycare.rule.modelauthority.model.AuthInfo;
import com.tansun.easycare.rule.rulemange.model.RuleManageVO;
import com.tansun.easycare.rule.rulemange.service.RuleManageService;
import com.tansun.easycare.rule.ruleversion.service.RuleVersioninfoService;

@RestController
@RequestMapping(value = "/ruleProj/Authority")
public class AuthorityController extends RuleBaseController {

    @Autowired
    private RuleManageService ruleManageService;
    @Autowired
    private RuleVersioninfoService ruleVersioninfoService;

    /**
     * 授权页面 决策表 决策树等非规则集的页面 决策表 决策树等的列表对象有父类，统一处理，规则流单独处理
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @param map      ModelMap
     * @param type     模块类型
     * @param code     编码
     * @return 结果信息
     */
    @RequestMapping("authorityRuleModuleAction")
    public ResponseVO authorityRuleModuleAction(HttpServletRequest request, HttpServletResponse response, @RequestBody AuthInfo authInfo) {
    	ResponseVO  res  = new  ResponseVO();
    	String authTypeValue = authInfo.getAuthTypeValue();
        String loginNames = "";
        String idList = authInfo.getId();
        //String ids[] = idList.split(",");
    	//for(String id:ids){//批量解冻、冻结
//        Principal principal = UserUtils.getPrincipal();
//
        String currLoginName = UserUtils.getSysService().getCurrUser().getLoginName();
        //String users = null;
        String projCode = authInfo.getProjectCode();
        try {
            this.ruleManageService.updateAuthority(idList, projCode, authTypeValue, loginNames, currLoginName);
        } catch (BizException e) {
            res.setStatus(ResponseVO.ERROR);
            res.setDescription(e.getMessage());
        }
    	//}
        return res;
    }

    /**
     * 授权页面 决策表 决策树等非规则集的页面
     *
     * @param manageid 代码
     * @return 页面路径
     */
    @RequestMapping("authorityRuleModulePage")
    public String authorityRuleModuleWin(String manageid, ModelMap map) throws Exception {
        //页面设置已经被授权的用户
        String users = null;
        BaseModel condition = new BaseModel();
        condition.setId(manageid);
        List<RuleManageVO> ruleList = ruleManageService.findRuleManageVO(condition);
        if (CollectionUtils.isEmpty(ruleList)) {
            throw new BizException("未找到规则模型");
        }
        RuleManageVO ruleManage = ruleList.get(0);
        users = ruleManage.getUsers();
        List<SysUser> userList = UserUtils.getSysService().getUserListByRole("");
        map.put("list", userList);
        map.put("users", users);
        return "rule/ruleproj/authortiy";
    }
}
